package cn.xshi.sys.web;

import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.base.BaseSearch;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.sys.model.SysKWords;
import cn.xshi.sys.service.SysKWordsService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Desc 关键词（敏感词）
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/sysKwords")
@Api(value = "关键词API",tags = "关键词API",description = "关键词API")
public class SysKWordsController extends BaseAction {

	@Resource
	private SysKWordsService sysKWordsService;

	/**
	* 加载初始化列表数据并分页
	* @param baseSearch
	*/
	@ApiOperation(value="查询关键词（敏感词）列表并分页", notes="查询关键词（敏感词）列表并分页")
	@PostMapping(value="/list")
	@Auth(value = "/sysKwords/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BasePage<List<SysKWords>> getSysKwordsListByCondition(@RequestBody(required=true) BaseSearch baseSearch){
		Map<String, Object> condition = baseSearch.convert();
		commonHPager(baseSearch);
		List<SysKWords> sysKWordsList = sysKWordsService.getSysKwordsListByCondition(condition);
//		for(SysKWords sysKWords : sysKWordsList){
//			if(!StringUtil.isEmpty(sysKWords.getCreateId())){
//				OauthAccountEntity createBy = getAccount(sysKWords.getCreateId());
//				if(null != createBy){
//					sysKWords.setCreateBy(createBy.getName());
//				}
//			}
//			if(!StringUtil.isEmpty(sysKWords.getUpdateId())){
//				OauthAccountEntity modifiedBy = getAccount(sysKWords.getUpdateId());
//				if(null != modifiedBy){
//					sysKWords.setModifiedBy(modifiedBy.getName());
//				}
//			}
//		}
		PageInfo<SysKWords> page = new PageInfo<SysKWords>(sysKWordsList);
		return outPageStr(page,baseSearch);
	}

	/**
	* 查询单个关键词（敏感词）
	* @param id
	*/
	@ApiOperation(value="查询单个关键词（敏感词）", notes="查询单个关键词（敏感词）")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/sysKwords/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<SysKWords> getSysKwordsById(@PathVariable("id")String id){
		SysKWords sysKWords = sysKWordsService.getSysKwordsById(id);
		if(!StringUtil.isEmpty(sysKWords.getCreateId())){
			OauthAccountEntity createBy = getAccount(sysKWords.getCreateId());
			if(null != createBy){
				sysKWords.setCreateBy(createBy.getName());
			}
		}
		if(!StringUtil.isEmpty(sysKWords.getUpdateId())){
			OauthAccountEntity modifiedBy = getAccount(sysKWords.getUpdateId());
			if(null != modifiedBy){
				sysKWords.setModifiedBy(modifiedBy.getName());
			}
		}
		return outDataStr(sysKWords);
	}

	/**
	* 添加
	* @param sysKWords
	*/
	@PostMapping(value="/add")
	@ApiOperation(value="创建单个关键词（敏感词）", notes="创建单个关键词（敏感词）")
	public BaseResult addSysKwords(@RequestBody SysKWords sysKWords){
		int i = 0;
		if(null != sysKWords){
			sysKWords.setId(toUUID());
			i= sysKWordsService.addSysKwords(sysKWords);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 修改
	* @param sysKWords
	*/
	@PutMapping(value="/update")
	@ApiOperation(value="编辑单个关键词（敏感词）", notes="编辑单个关键词（敏感词）")
	public BaseResult updateSysKwords(@RequestBody SysKWords sysKWords){
		int i = 0;
		if(null != sysKWords){
			i= sysKWordsService.updateSysKwords(sysKWords);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 删除
	* @param id
	*/
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除关键词（敏感词）", notes="删除关键词（敏感词）")
	public BaseResult delSysKwords(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i= sysKWordsService.delSysKwords(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
}
